package com.wwwarehouse.usercenter.fragment.permissiondistribute;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.fastjson.JSON;
import com.wwwarehouse.common.activity.base.BaseFragment;
import com.wwwarehouse.common.bean.response.CommonClass;
import com.wwwarehouse.common.custom_widget.time_pick.ChoosePickerDialog;
import com.wwwarehouse.common.http.NoHttpUtils;
import com.wwwarehouse.common.tools.LogUtils;
import com.wwwarehouse.common.tools.ToastUtils;
import com.wwwarehouse.usercenter.R;
import com.wwwarehouse.usercenter.bean.authoritydistribute.BusinessUnitIdBean;
import com.wwwarehouse.usercenter.constant.UserCenterConstant;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * @author : mundane
 * @time : 2017/9/24 16:47
 * @description :
 * @file : FakeAuthorityEntranceFragment.java
 */
@Route(path = UserCenterConstant.PATH_FAKE_ENTRANCE)
public class FakeAuthorityEntranceFragment extends BaseFragment {

	private View mBtn;

	private List<BusinessUnitIdBean> mBusinessUnitIdBeanList;
	private List<String> mStringList;
	private HashMap<String, String> mNameAndIdMap;
	private NoHttpUtils.OnResponseListener mResponseListener = new NoHttpUtils.OnResponseListener() {

		@Override
		public void onSucceed(CommonClass commonClass, int what) {
			switch (what) {
				case 0:
					if (TextUtils.equals("0", commonClass.getCode())) {
						LogUtils.showInfoLog("commonClass = " + commonClass.getData().toString());
						mBusinessUnitIdBeanList = JSON.parseArray(commonClass.getData().toString(), BusinessUnitIdBean.class);
						for (BusinessUnitIdBean businessUnitIdBean : mBusinessUnitIdBeanList) {
							mStringList.add(businessUnitIdBean.buName);
							mNameAndIdMap.put(businessUnitIdBean.buName, businessUnitIdBean.buId);
						}
					} else {
						ToastUtils.showToast(commonClass.getMsg());
					}
					break;
			}


		}

		@Override
		public void onFailed(String s, int i) {
			if (TextUtils.isEmpty(s)) {
				Toast.makeText(mActivity, "服务器挂了", Toast.LENGTH_SHORT).show();
			} else {
				LogUtils.showErrLog(s);
			}
		}

		@Override
		public void onFinish(int i) {

		}
	};

	public static FakeAuthorityEntranceFragment newInstance() {
		FakeAuthorityEntranceFragment fragment = new FakeAuthorityEntranceFragment();
		Bundle args = new Bundle();
		fragment.setArguments(args);
		return fragment;
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		if (getArguments() != null) {

		}
		mStringList = new ArrayList<>();
		mNameAndIdMap = new HashMap<>();
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.fragment_entrance_authority_fake, container, false);
		return view;
	}

	@Override
	public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
		// findViewById
		mBtn = $(R.id.btn);
		mBtn.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
                new ChoosePickerDialog.Builder(mActivity)
						.setData(mStringList)
						.setTitle("选择业务单位")
						.setOnDataSelectedListener(new ChoosePickerDialog.OnSelectedListener() {
					@Override
					public void onSelected(String s, int i) {
						// TODO: 2017/9/25
						pushFragment(AuthorityDistributePagerFragment.newInstance(mNameAndIdMap.get(s)));
//						pushFragment(BRCFDTagsFragment.newInstance("10001357"));
					}

					@Override
					public void onCancel() {

					}
				})
						.create()
						.show();
			}
		});
	}

	@Override
	public void setTitle() {
		if (peekFragment() instanceof FakeAuthorityEntranceFragment) {
			mActivity.setTitle("权限分配假入口");
		}
	}

	@Override
	public void requestDatas() {
		Map<String, String> map = new HashMap<>();
		map.put("data", null);
		NoHttpUtils.httpPost(UserCenterConstant.GET_BUSINESSUNITS_BY_USERID, map, mResponseListener, 0);
	}
}
